
//第一句
//__DECLARE_COMSINK_BEGIN(CComSink, IComSink, IComSink)
template <class CHost>       //CHost是自己定义的业务
class __multiple_inheritance CComSink; 

template <class CHost> 
class CComSinkPtr : public CComSinkPtr<CComSink<CHost> > 
{ 
    public: 
    CComSinkPtr(CHost* pHost) : CComSinkPtr<CComSink<CHost> >(pHost) {} 
    CComSinkPtr(const AsyncTask::WeakPtr<CHost>& pHost) : CComSinkPtr<CComSink<CHost> >(pHost) {}
    virtual HRESULT FinalConstruct(){return S_OK;}; 
    virtual void FinalRelease() {return;};
}; 

template <class CHost> 
class CComSink: public CComObjectRootEx<CComMultiThreadModel>, public IComSink 
{
    friend class CComSinkPtr<CComSink<CHost> >;

public: 
typedef CHost CHostType;
CComSink(CHost* pHost) :m_pHost(pHost) 
{
    _AtlInitialConstruct(); 
    memset((BYTE*)&m_pWeakHost + sizeof(m_pWeakHost), 0, sizeof(*this) - ((BYTE*)&m_pWeakHost - (BYTE*)this) - sizeof(m_pWeakHost));
}

CComSink(const AsyncTask::WeakPtr<CHost>& pHost) : m_pWeakHost(pHost), m_pHost(0)
{
    _AtlInitialConstruct(); 
    memset((BYTE*)&m_pWeakHost + sizeof(m_pWeakHost), 0, sizeof(*this) - ((BYTE*)&m_pWeakHost - (BYTE*)this) - sizeof(m_pWeakHost));
}

~CComSink()
{
    UnhookAll(); 
    m_pHost = 0; 
} 
virtual HRESULT FinalConstruct(){return S_OK;}; 
virtual void FinalRelease() {return;};

public: 
void UnhookAll() 
{
    for (BYTE * p = (BYTE*)&m_pWeakHost + sizeof(m_pWeakHost); p < (BYTE*)this + sizeof(*this); p += sizeof(FWeakCallback*)) 
    { 
        FWeakCallback* pF = *((FWeakCallback**)p); delete pF; 
    }
    memset((BYTE*)&m_pWeakHost + sizeof(m_pWeakHost), 0, sizeof(*this) - ((BYTE*)&m_pWeakHost - (BYTE*)this) - sizeof(m_pWeakHost)); 
}

private: 
    CHost* m_pHost;
    AsyncTask::WeakPtr<CHost> m_pWeakHost; 
private:
CComSink& operator=(const CComSink& rhs);
CComSink(const CComSink& rhs);



//__DECLARE_COMSINK_INTERFACE_BEGIN(CComSink)
public: 
public: 
typedef CComSink<CHost> _ComMapClass; 
static HRESULT WINAPI _Cache(  void* pv,   REFIID iid,   void** ppvObject,   DWORD_PTR dw) 
throw() 
{
    _ComMapClass* p = (_ComMapClass*)pv; p->Lock(); 
    HRESULT hRes = E_FAIL; __try { hRes = ATL::CComObjectRootBase::_Cache(pv, iid, ppvObject, dw); } 
    __finally { p->Unlock(); } return hRes; 
} 
IUnknown* _GetRawUnknown() 
throw() 
{
    ATLASSERT(_GetEntries()[0].pFunc == _ATL_SIMPLEMAPENTRY);
    return (IUnknown*)((INT_PTR)this+_GetEntries()->dw); 
}
IUnknown* GetUnknown() throw() {return _GetRawUnknown();}

HRESULT _InternalQueryInterface(   REFIID iid,   void** ppvObject) 
throw() { return this->InternalQueryInterface(this, _GetEntries(), iid, ppvObject); } 

const static ATL::_ATL_INTMAP_ENTRY* WINAPI _GetEntries() 
throw()
{ 
    static const ATL::_ATL_INTMAP_ENTRY _entries[] = {


    //__IMPLEMENT_COMSINK_INTERFACE(IComSink)
                        {&_ATL_IIDOF(IComSink), offsetofclass(IComSink, _ComMapClass), _ATL_SIMPLEMAPENTRY},



    //__DECLARE_COMSINK_INTERFACE_END()
    __if_exists(_GetAttrEntries) 
    {
        {0, (DWORD_PTR)_GetAttrEntries, _ChainAttr }, 
    } 
    {0, 0, 0}
    };

    return _entries;
} 

virtual ULONG STDMETHODCALLTYPE AddRef(void) throw() = 0; 
virtual ULONG STDMETHODCALLTYPE Release(void) throw() = 0; 
STDMETHOD(QueryInterface)( REFIID,   void**) throw() = 0;



//第二句
//__IMPLEMENT_COMSINK_FUNCTION(func, params, values)



//第三句
//__DECLARE_COMSINK_END()
};





